﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Data;
using System.Data.SqlClient;
using System.Data.Sql;

namespace DAL
{
    public class DanhMucLoaiSanPhamQuery : DataProvide
    {
        public List<DanhMucLoaiSanPhamDAL> LayDanhMucLoaiSanPham()
        {
            List<DanhMucLoaiSanPhamDAL> dmloaiSanPham = new List<DanhMucLoaiSanPhamDAL>();
            SqlConnection chuoiKetNoi = null;
            try
            {
                base.Connect();
                chuoiKetNoi = connection;
                string sqlString = String.Format(@"SELECT * FROM DanhMucLoaiSanPham");

                SqlDataReader dr = ExecuteReader(sqlString);
                while (dr.Read())
                {
                    DanhMucLoaiSanPhamDAL lsp = (DanhMucLoaiSanPhamDAL)GetDataRowFromDataReader(dr);
                    dmloaiSanPham.Add(lsp);
                }
            }/*
            catch
            {
                return null;
            }*/
            finally
            {
                if (chuoiKetNoi != null && chuoiKetNoi.State == System.Data.ConnectionState.Open)
                    chuoiKetNoi.Close();
            }
            return dmloaiSanPham;
        }

        public DanhMucLoaiSanPhamDAL LayLoaiSPTheoMaLoai(string malsp)
        {
            DanhMucLoaiSanPhamDAL loaiSanPham = new DanhMucLoaiSanPhamDAL();
            SqlConnection chuoiKetNoi = null;
            try
            {
                base.Connect();
                chuoiKetNoi = connection;
                string sqlString = String.Format(@"SELECT * FROM DanhMucLoaiSanPham WHERE MaLoaiSanPham ='"+ malsp+"'");

                SqlDataReader dr = ExecuteReader(sqlString);
                while (dr.Read())
                {
                    //DanhMucLoaiSanPhamDAL lsp = (DanhMucLoaiSanPhamDAL)GetDataRowFromDataReader(dr);
                    //dmloaiSanPham.Add(lsp);
                    loaiSanPham = (DanhMucLoaiSanPhamDAL)GetDataRowFromDataReader(dr);
                }
            }/*
            catch
            {
                return null;
            }*/
            finally
            {
                if (chuoiKetNoi != null && chuoiKetNoi.State == System.Data.ConnectionState.Open)
                    chuoiKetNoi.Close();
            }
            return loaiSanPham;
        }

        protected override object GetDataRowFromDataReader(SqlDataReader dr)
        {
            DanhMucLoaiSanPhamDAL dmloaiSanPham = new DanhMucLoaiSanPhamDAL();
            dmloaiSanPham.MaLoaiSanPham = dr.GetString(0);
            if (!dr.IsDBNull(1))
                dmloaiSanPham.TenLoaiSanPham = dr.GetString(1);
            if (!dr.IsDBNull(2))
                dmloaiSanPham.ThongTinRieng = dr.GetString(2);
            return (object)dmloaiSanPham;
        }
    }
}
